package yiwang.rememberdaka;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by tony on 16/4/15.
 */
public class CheckIOActivity extends Activity {
    private String seesionKey;
    private Button btnCheckIn;//签到
    private Button btnCheckOut;//签退

    private OnCheckListener onCheckListener = new OnCheckListener() {
        @Override
        public void onCheckResult(boolean isCheckSuccess, int checkType, String json) {
            if(!isCheckSuccess){
                Toast.makeText(CheckIOActivity.this, checkType == Constant.CHECK_IN ? "上班签到失败" : "下班签退失败", Toast.LENGTH_SHORT).show();
                return;
            }

            String result = parseCheckInfo(json);
            if(result == null || result.equals("")){
                Toast.makeText(CheckIOActivity.this, "服务器返回数据错误！", Toast.LENGTH_SHORT).show();
                return;
            }

            if(result.equals("success")){
                Toast.makeText(CheckIOActivity.this, checkType == Constant.CHECK_IN ? "上班签到成功" : "下班签退成功", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkio);
        seesionKey = getIntent().getStringExtra(Constant.MSG_SEESIONKEY);
        initView();
    }

    private void initView(){
        btnCheckIn = (Button) findViewById(R.id.btnCheckin);
        btnCheckOut = (Button) findViewById(R.id.btnCheckout);

        btnCheckIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check(Constant.CHECK_IN, onCheckListener);
            }
        });

        btnCheckOut.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                check(Constant.CHECK_OUT, onCheckListener);
            }
        });

    }

    private void check(final int checkTpye, final OnCheckListener onCheckListener){
        if(onCheckListener == null){
            return;
        }

        String address;
        try {
            address = URLEncoder.encode(Constant.USER_ADDRESS, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return;
        }

        RequestParams params = new RequestParams();
        params.addBodyParameter("method", "checkin");
        params.addBodyParameter("type", checkTpye == Constant.CHECK_IN ? "checkin" : "checkout");
        params.addBodyParameter("latlng", "31.201415,121.581484");
        params.addBodyParameter("addr", address);
        params.addBodyParameter("sessionkey",seesionKey);

        String checkt = ((checkTpye == Constant.CHECK_IN) ? "checkin" : "checkout");

        String url = Constant.URL + "?"+"method=checkin"+"&type="+checkt
                +"&latlng=31.201407,121.581486"
                +"&addr="+address
                +"&sessionkey="+seesionKey;

        Constant.HTTP_UTILS.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException httpException, String arg1) {
                onCheckListener.onCheckResult(false, checkTpye, null);
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String jsonResult = responseInfo.result;
                onCheckListener.onCheckResult(true, checkTpye, jsonResult);
            }
        });
    }

    private String parseCheckInfo(String json){
        if (json == null)
            return null;

        String result = "";
        try {
            JSONTokener jsonTokener = new JSONTokener(json);
            JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
            result = jsonObject.optString("result");
        } catch (JSONException e) {
            e.printStackTrace();
        }finally {
            return result;
        }
    }


}
